<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="入库编号" prop="inoutCode">
        <el-input
          v-model="queryParams.inoutCode"
          placeholder="请输入入库编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单编号" prop="orderCode">
        <el-input
          v-model="queryParams.orderCode"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['stock:in:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['stock:in:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['stock:in:remove']"
        >删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="inoutList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="序号" align="center" prop="inoutId"/>
      <el-table-column label="入库编号" align="center" prop="inoutCode"/>
      <el-table-column label="订单编号" align="center" prop="orderCode"/>
      <el-table-column label="入库状态" align="center" prop="status">
        <template slot-scope="scope">
          <span v-if="scope.row.status == 0">未入库</span>
          <span v-else>已入库</span>
        </template>
      </el-table-column>
      <el-table-column label="创建者" align="center" prop="createBy" width="120"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['stock:in:edit']"
            v-show="scope.row.status==0"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['stock:in:remove']"
            v-show="scope.row.status==0"
          >删除
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-warning"
            @click="handleIn(scope.row)"
            v-hasPermi="['stock:in:in']"
            v-show="scope.row.status==0"
          >入库
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-search"
            @click="handleBodyQuery(scope.row)"
            v-show="scope.row.status!=0"
          >查看
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改入库对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="780px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="入库编号" prop="inoutCode">
              <el-input v-model="form.inoutCode" placeholder="入库编号自动生成" :disabled="true"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="采购订单" prop="sourceId" v-show="form.inoutId == null">
              <el-select v-model="form.sourceId" placeholder="请选择采购订单" @change="orderChange"
                         style="width: 100%;"
              >
                <el-option
                  v-for="order in orderOptions"
                  :key="order.orderId"
                  :label="order.orderCode + ' - ' + order.orderName"
                  :value="order.orderId"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="采购订单" prop="orderCode" v-show="form.inoutId != null">
              <el-input v-model="form.orderCode" :disabled="true"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <el-divider content-position="center">采购入库子信息</el-divider>
      <el-table :data="form.inoutBodyList" :row-class-name="rowInoutBodyIndex">
        <el-table-column label="序号" align="center" prop="index" width="100"/>
        <el-table-column label="仓库" align="center" prop="storeId" width="220">
          <template slot-scope="scope">
            <el-select v-model="scope.row.storeId" placeholder="请选择仓库" v-if="form.status==0">
              <el-option
                v-for="store in storeOptions"
                :key="store.storeId"
                :label="store.storeName"
                :value="store.storeId"
              />
            </el-select>
            <span v-for="store in storeOptions" v-if="form.status!=0">
              <span v-if="scope.row.storeId == store.storeId">{{ store.storeName }}</span>
            </span>
          </template>
        </el-table-column>
        <el-table-column label="存货管理" align="center" prop="inventoryId" width="200">
          <template slot-scope="scope">
            <span v-for="inventory in inventoryOptions">
              <span v-if="scope.row.inventoryId == inventory.inventoryId">{{ inventory.inventoryName }}</span>
            </span>
          </template>
        </el-table-column>
        <el-table-column label="数量" align="center" prop="orderNumber" width="220"/>
      </el-table>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="form.status==0">确 定</el-button>
        <el-button @click="cancel" v-if="form.status==0">取 消</el-button>
        <el-button @click="cancel" v-if="form.status!=0">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addInout, delInout, getInout, getInoutByOrder, inoutIn, listInout, updateInout } from '@/api/stock/in'
import { listOrder } from '@/api/purchase/order'
import { listStore } from '@/api/archive/store'
import { listInventory } from '@/api/archive/inventory'

export default {
  name: 'Inout',
  dicts: ['sys_inout_status', 'sys_inout_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 入库表格数据
      inoutList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        inoutTitle: undefined,
        createBy: undefined,
        status: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        sourceId: [
          { required: true, message: '采购订单不能为空', trigger: 'blur' }
        ]
      },
      // 采购订单选项
      orderOptions: [],
      storeOptions: [],
      inventoryOptions: []
    }
  },
  created() {
    this.getList()
    this.getStoreList()
    this.getInventoryList()
  },
  methods: {
    getOrderList() {
      const queryParams = {}
      queryParams.approveStatus = '2'
      queryParams.isIn = '0'
      listOrder(queryParams).then(res => {
        this.orderOptions = res.data
      })
    },
    getStoreList() {
      listStore().then(res => {
        this.storeOptions = res.data
      })
    },
    getInventoryList() {
      listInventory().then(res => {
        this.inventoryOptions = res.data
      })
    },
    /** 查询入库列表 */
    getList() {
      this.loading = true
      listInout(this.queryParams).then(response => {
        this.inoutList = response.data
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        inoutId: undefined,
        inoutCode: undefined,
        orderCode: undefined,
        sourceId: undefined,
        inoutBodyList: []
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.inoutId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.getOrderList()
      this.open = true
      this.title = '添加入库'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const inoutId = row.inoutId || this.ids
      getInout(inoutId).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改入库'
      })
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.inoutId != undefined) {
            updateInout(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addInout(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const inoutIds = row.inoutId || this.ids
      this.$modal.confirm('是否确认删除入库编号为"' + inoutIds + '"的数据项？').then(function() {
        return delInout(inoutIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 采购入库单子序号 */
    rowInoutBodyIndex({ row, rowIndex }) {
      row.index = rowIndex + 1
    },
    /** 采购订单切换*/
    orderChange() {
      getInoutByOrder(this.form.sourceId).then(res => {
        this.form = res.data
      })
    },
    /** 入库按钮 */
    handleIn(row) {
      this.$modal.confirm('是否确认入库编号为"' + row.inoutCode + '"的数据项？').then(function() {
        return inoutIn(row)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('入库成功')
      }).catch(() => {
      })
    },
    /** 查看按钮 */
    handleBodyQuery(row) {
      this.reset()
      const inoutId = row.inoutId || this.ids
      getInout(inoutId).then(response => {
        this.form = response.data
        this.open = true
        this.title = '查看入库'
      })
    }
  }
}
</script>
